package astra.client;

import astra.client.models.*;
import astra.client.pages.MainLayout;
import astra.client.pages.clients.ClientPopup;
import astra.client.pages.employee.EmployeePopup;
import astra.client.pages.supplier.SupplierPopup;
import astra.client.services.AstraService;
import astra.client.services.AstraServiceAsync;
import astra.client.utils.dialogs.AuthDialog;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.Map;

/**
 * Entry point classes define <code>onModuleLoad()</code>
 */
public class Astra implements EntryPoint, AsyncCallback<Employee> {
    public static Astra astra;
    public final static AstraServiceAsync service = GWT.create(AstraService.class);

    private Employee employee;
    private AuthDialog dialogBox;
    private RootPanel panel;
    private MainLayout mainLayout;
    private RootPanel actionsPanel;
    private AstraData astraData;

    private ClientPopup clientPopup;
    private SupplierPopup supplierPopup;
    private EmployeePopup employeePopup;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        clientPopup = new ClientPopup(astra, service);
        supplierPopup = new SupplierPopup(astra, service);
        employeePopup = new EmployeePopup(astra, service);

        panel = RootPanel.get("content");
        actionsPanel = RootPanel.get("actions");
		dialogBox = new AuthDialog(service, this);
		service.getEmployee(this);
        astra = this;
    }

    public void onSuccess(Employee result) {
        employee = result;
        dialogBox.hide();
        if (mainLayout == null) {
            mainLayout = new MainLayout(this);
            panel.add(mainLayout);
        }
        mainLayout.setVisible(false);
        service.getAstraData(new AsyncCallback<AstraData>(){
            @Override
            public void onFailure(Throwable caught) {
                astra.onFailure(caught);
            }

            @Override
            public void onSuccess(AstraData result) {
                astraData = result;
                mainLayout.setVisible(true);
            }
        });
    }

    public void onSuccess(String info, SimplePanel messagePanel) {
        if (messagePanel != null) {
            messagePanel.addStyleName("info");
            messagePanel.removeStyleName("error");
            if (info != null) {
                messagePanel.setWidget(new Label(info));
            } else {
                messagePanel.setWidget(new Label("Внутреняя ошибка сервиса"));
            }
        } else {
            if (info != null) {
                Window.alert(info);
            } else {
                Window.alert("Внутреняя ошибка сервиса");
            }
        }
    }

    public void onFailure(Throwable caught, String error, SimplePanel messagePanel) {
        if (caught != null && "Access denied".equals(caught.getMessage())) {
			dialogBox.show();
            dialogBox.center();
		} else if (messagePanel != null) {
            messagePanel.removeStyleName("info");
            messagePanel.addStyleName("error");
            if (error != null) {
                messagePanel.setWidget(new Label(error));
            } else {
                messagePanel.setWidget(new Label("Внутреняя ошибка сервиса"));
            }
        } else {
            if (error != null) {
                Window.alert(error);
            } else {
                Window.alert("Внутреняя ошибка сервиса");
            }
        }
    }

    public void onFailure(Throwable caught) {
        onFailure(caught, null, null);
    }

    public Employee getEmployee() {
        return employee;
    }

    public AstraData getAstraData() {
        return astraData;
    }

    public void setActions(FlowPanel actions) {
        if (actions != null) {
            actionsPanel.clear();
            actionsPanel.add(actions);
            actionsPanel.setVisible(true);
        } else {
            actionsPanel.setVisible(false);
        }
    }

    public ClientPopup getClientPopup() {
        return clientPopup;
    }

    public SupplierPopup getSupplierPopup() {
        return supplierPopup;
    }

    public EmployeePopup getEmployeePopup() {
        return employeePopup;
    }
}
